import { getCardAttr } from './attr';
import type { BattleCard, Skill, SkillInfo, CardAction, Buff } from '../types';

export const buffAction = (card: BattleCard, skl: SkillInfo, target: BattleCard[]) => {
  const action: CardAction = {
    from: card.from,
    to: target.map(v => v.from)[0],
    id: card.base.id,
    avatar: card.base.heroAvatar,
    skill: skl.skill.id,
    skillType: skl.type,
    angry: 0,
    target: [],
  };

  // 出手后怒气处理 - 连大，先清空，后补充
  if (skl.type === 2) {
    card.battle.ANGRY = 0;
  }

  // 增益开始
  for (let i = 0; i < target.length; i++) {
    const v = target[i];
    console.log(`目标:${v.base.heroAvatar}`);
    const additionalNum = skl.skill.additional;
    const buff: Buff[] = [];

    for (let j = 1; j <= additionalNum; j++) {
      const logicKey = skl.skill[`logic${j}` as keyof Skill] as number;
      const logicType = skl.skill[`logic${j}_type` as keyof Skill] as number;
      const logicValue = skl.skill[`logic${j}_parameter` as keyof Skill] as number;
      const logicRound = skl.skill[`logic${j}_round` as keyof Skill] as number;

      const newBuff = {
        BUFF: logicKey,
        EFF: logicType,
        TYPE: logicValue > 100 ? 1 : 0,
        VAL: logicValue,
        RND: logicRound,
      };

      v.buff.push(newBuff);

      buff.push(newBuff);

      if (logicKey < 50) {
        console.log(`效果：${logicKey}`);
        console.log(`类型：${logicType}`);
        console.log(`数值：${logicValue}`);
        console.log(`回合：${logicRound}`);
        v.battle = getCardAttr(v);
        console.log(`目标属性：${JSON.stringify(v.battle)}`);
      } else if (logicKey === 50) {
        console.log(`效果：${logicKey}-怒气增加`);
        console.log(`效果：buff前怒气:${v.battle.ANGRY}`);
        v.battle.ANGRY = v.battle.ANGRY + logicValue > 100 ? 100 : v.battle.ANGRY + logicValue;
        console.log(`效果：buff后怒气:${v.battle.ANGRY}`);
      }
    }

    action.target.push({
      from: v.from,
      id: v.base.id,
      action: skl.skill.action,
      buff: buff,
      hit: 1,
      crit: 0,
      dmg: 0,
      die: 0,
      recover: 0,
      hp: v.battle.HP,
      angry: v.battle.ANGRY,
    });
  }

  card.battle.ANGRY = card.battle.ANGRY + 10 > 100 ? 100 : card.battle.ANGRY + 10;
  action.angry = card.battle.ANGRY;

  return action;
};
